package web.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import utils.OperatorUtil;
import utils.UrlUtils;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SeleniumAction extends ActionSupport {

    private HttpServletRequest request = ServletActionContext.getRequest();
    //用户需要解析的内容
    private List<String> type;
    private String field;

    @Test
    public void test() throws AWTException {
        WebDriver webDriver = getWebDriver();
        OperatorUtil.open(webDriver, "https://www.baidu.com");
        OperatorUtil.waitTime(2000);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_S);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        OperatorUtil.waitTime(2000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        webDriver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "s"));
        System.out.println(webDriver.getTitle());
    }

    //模拟登录的方法
    public String simulation() {
//        System.out.println("调用了模拟登录的方法");
        String url = request.getParameter("url");
        WebDriver webDriver = getWebDriver();
        OperatorUtil.open(webDriver, UrlUtils.getRequestUrl(url));
        OperatorUtil.waitTime(2000);


//        System.out.println(url);
        return NONE;
    }

    //解析页面的方法
    public String analysis() {
        String url = request.getParameter("url");
        return NONE;
    }


    public String login() {
//        HttpServletRequest request = ServletActionContext.getRequest();
        //获得登录表单传过来的参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //获得所有的name
        Set<String> keySet = parameterMap.keySet();
        //构建浏览器对象
        WebDriver webDriver = getWebDriver();
        //遍历name,以获得value
        for (String key : keySet) {
            if ("actualUrl".equals(key)) {
                //浏览器实际访问的url
                OperatorUtil.open(webDriver, parameterMap.get(key).toString());
            } else {
                //表单登录所需要的参数
                webDriver.findElement(new By.ByName(key)).sendKeys(parameterMap.get(key).toString());
            }

        }
        //参数填充完毕
        webDriver.findElement(new By.ByCssSelector("input[type=submit]")).click();

        //等待浏览器加载完成
        OperatorUtil.waitTime(3000);
        //跳转到最新的页面
        webDriver = OperatorUtil.switchToNewWindow(webDriver);
        //到达正确的页面开始解析
        DownLoad(webDriver);
        return NONE;
    }

    //解析页面
    private void DownLoad(WebDriver webDriver) {
        if (type.contains("img")) {
//                System.out.println("用户选择了图片");
            //当用户选择了解析图片的选项时,获得所有的img标签

            //把获取到的所有img标签写入一个jsp文件中,用户可勾选出自己想要的img标签

        }
        if (type.contains("table")) {
//                System.out.println("用户选择了表单");
        }
        if (type.contains("text")) {
//                System.out.println("用户选择了文本,筛选条件是:" + field);
        }

    }

    //直接跳转到了正确的页面,直接开始解析
    public void DownLoad(String url) {
        WebDriver webDriver = getWebDriver();
        OperatorUtil.open(webDriver, url);
        DownLoad(webDriver);
    }

    private WebDriver getWebDriver() {
        /*ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");
        return new ChromeDriver(options);*/
        return new ChromeDriver();
    }

    public void setType(List<String> type) {
        this.type = type;
    }


    public void setField(String field) {
        this.field = field;
    }
}
